﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Silvermoon.OpenGL;

namespace Silvermoon.Shapes
{
    /// <summary>
    /// Helper class to create vertex arrays.
    /// </summary>
    public static class VertexUtil
    {
        /// <summary>
        /// Returns a vertex array for a rectangle divided into 4 quadrants.
        /// 0...1
        /// 2...3
        /// </summary>
        public static short[] Create4QuadArray(Size Size, Texture texture)
        {
            short width = (short)Size.Width;
            short height = (short)Size.Height;

            Size size = texture != null ? texture.StorageSize : new Size(2 * width / 3, 2 * height / 3);
            if (size.Width > Size.Width) size.Width = Size.Width;
            if (size.Height > Size.Height) size.Height = Size.Height;
            short tw = (short)(size.Width / 2);
            short th = (short)(size.Height / 2);

            return new short[]
            {
                0,0, tw,0, 0,th, tw,th,

                (short)(width-tw),0, width,0, (short)(width-tw),th, width,th,

                0,(short)(height-th), tw,(short)(height-th), 0,(short)height, tw,(short)height,

                (short)(width-tw),(short)(height-th),width,(short)(height-th), (short)(width-tw),(short)height, width,(short)height
            };
        }

        /// <summary>
        /// Returns a vertex array for a rectangle divided into 6 quadrants
        /// 01
        /// 23
        /// 45
        /// </summary>
        public static short[] Create6QuadArray(Size Size, Texture texture)
        {
            short width = (short)Size.Width;
            short height = (short)Size.Height;

            Size size = texture != null ? texture.StorageSize : new Size(width / 2, height / 2);
            if (size.Width > Size.Width) size.Width = Size.Width;
            if (size.Height > Size.Height) size.Height = Size.Height;
            short tw = (short)(size.Width / 2);
            short th = (short)(size.Height / 2);

            short tw1 = (short)(width - tw);
            short th1 = (short)(height - th);
            short c = (short)(height / 2);
            return new short[]
            {
                0,0, tw,0, tw1,0, width,0,
                0,th,tw,th,tw1,th,width,th,
                0,c,tw,c,tw1,c,width,c,
                0,c,tw,c,tw1,c,width,c,
                0,th1,tw,th1,tw1,th1,width,th1,
                0,height,tw,height,tw1,height,width,height
            };
        }

        public static short[] SquaredArray(Size size)
        {
            short right = (short)(size.Width);
            short bottom = (short)(size.Height);

            return new short[] 
                    { 
                        right, bottom,
                        right, 0,
                        0, bottom,
                        0, 0 
                    };
        }
    }
}
